package com.super8.super8newtv.api.parser;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.super8.super8newtv.api.ApiError;
import com.super8.super8newtv.bean.ResponseBean;
import com.super8.super8newtv.utils.LogUtils;

import java.lang.reflect.Type;

public class ResponseBeanParser<T> implements IParserJson {

    private Type typeOfT;

    public ResponseBeanParser(Type rdType) {
        typeOfT = rdType;
    }

    @Override
    public ResponseBean<T> parse(String jsonStr) throws ApiError {
        try {
            LogUtils.e("ResultDataParser接口数据转换", "jsonStr:" + jsonStr);
            ResponseBean<T> resultData = new ResponseBean<T>();
            Gson gson = new GsonBuilder().setDateFormat("yyyy/MM/dd HH:mm:ss").create();
            resultData = (ResponseBean<T>) gson.fromJson(jsonStr, typeOfT);
            return resultData;
        } catch (Exception e) {
            e.printStackTrace();
            LogUtils.e("ResultDataParser接口数据转换_异常", "jsonStr:" + jsonStr + "————Json parse error:" + e);//数据转换异常日志
            ApiError error = new ApiError(ApiError.ErrorCode.JSON_PARSE_ERROR, "Json parse error:" + jsonStr, e);
            throw error;
        }
    }
}
